package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Auction;
import com.ericom.service.GenericManager;

public class AuctionAction extends BaseAction {

	private GenericManager<Auction, Long> auctionManager;
	private List auctions;
	private Auction auction;
	private Long id;

	public Auction getAuction() {
		return auction;
	}

	public void setAuction(Auction auction) {
		this.auction = auction;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setAuctionManager(GenericManager<Auction, Long> auctionManager) {
		this.auctionManager = auctionManager;
	}

	public String list() {
		auctions = auctionManager.getAll();
		return SUCCESS;
	}

	public List getAuctions() {
		return auctions;
	}

	public String delete() {
		auctionManager.remove(auction.getId());
		this.saveMessage(getText("auction.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			auction = auctionManager.get(id);
		} else {
			auction = new Auction();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (auction.getId() == null);
		auction = auctionManager.save(auction);

		String key = (isNew) ? "auction.added" : "auction.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
